<?php
/**
 * Pertyler
 * Table Page
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require 'main.php';

if((isset($page->Table)) AND (isset($page->Row)) AND (isset($page->Column)))
{
	$table = Pertyler_Table::Select($page->Table);

	$row = $table->SelectRow($page->Row);

	$columnArray = $row->SelectGroupColumnArray();

	$column = $columnArray[$page->Column];

	$tree = $page->Tree($row);

	$branchlet = $tree->Add(new TreeBranchlet($column));

	$branchlet->AddParameter('ParentTable', $page->Table);

	if($branchlet->Pressed() == 'delete')
	{
		$row->UpdateAsRemovedFromGroup($column);

		$tree->AddProperty('Removed');
	}
	else if($branchlet->Pressed() == 'add_okay')
	{
		$inputRow = $table->SelectRow($_POST['row']);

		$inputRow->UpdateAsJoinedIntoGroup($column, $row);

		$tree->AddProperty('Added');
	}
	else if($branchlet->Pressed() == 'change_okay')
	{
		$row->UpdateAsJoinedIntoGroup($column, $table->SelectRow($_POST['row']));

		$tree->AddProperty('Joined');
	}

	if($branchlet->Pressed() == 'add')
	{
		$branchlet->AddButton('add_okay');

		$branchlet->AddButton('cancel');

		$html = '
<select name="row" >';

		foreach($table->SelectGrouplessRowArray($column) AS $row)
			$html .= '
	<option value="' . $row->id() . '" >
		' . $row . '
	</option>';

		$html .= '
</select>';

		$branch = $branchlet->Add(new TreeBranch($table, $html));

		if($table->SelectPrivilege('approve'))
		{
			$leaf = $branch->Add(new TreeLeaf('Last Changed Time', new Date));

			if($table->SelectPrivilege('approve'))
				$leaf->Colour('#006666');

			$leaf = $branch->Add(new TreeLeaf('Last Changed Moderator', Pertyler_Role_Moderator::Select()));
		}
	}
	else if($branchlet->Pressed() == 'change')
	{
		$branchlet->AddButton('change_okay');

		$branchlet->AddButton('cancel');

		if($table->SelectPrivilege('approve'))
		{
			$leaf = $tree->Add(new TreeLeaf('Last Changed Time', new Date));

			if($table->SelectPrivilege('approve'))
				$leaf->Colour('#006666');

			$leaf = $tree->Add(new TreeLeaf('Last Changed Moderator', Pertyler_Role_Moderator::Select()));
		}

		$html = '
<select name="row" >';

		foreach($table->SelectRowArray() AS $row)
			$html .= '
	<option value="' . $row->id() . '" >
		' . $row . '
	</option>';

		$html .= '
</select>';
	
		$branchlet->Add(new TreeLeaf($table, $html));
	}
	else
	{
		$columnArray = $row->SelectGroupColumnArray();

		$column = $columnArray[$page->Column];

		if($column->Value())
		{
			if(($table->SelectPrivilege('select')) AND (Pertyler_Role::Select()->SelectRolePrivilege($table->SelectPrivilege('select'))))
				foreach($row->SelectGroupRowArray($column) AS $row)
				{
					$leaf = $branchlet->Add(new TreeLeaf($row));

					$leaf->AddParameter('Row', $row->id());

					if($table->SelectTableArray())
						$leaf->AddLink('ObjectBrowser.php', 'leftMenu');
					else
						$leaf->AddLink('row.php');
				}

			if(($table->SelectPrivilege('update')) AND (Pertyler_Role::Select()->SelectRolePrivilege($table->SelectPrivilege('update'))))
			{
				$branchlet->AddButton('delete');

				$branchlet->AddButton('add');
			}
		}
		else
			if(($table->SelectPrivilege('update')) AND (Pertyler_Role::Select()->SelectRolePrivilege($table->SelectPrivilege('update'))))
				$branchlet->AddButton('change');
	}

	echo $tree;
}